﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using Service.Core;
using Service.Core.Interface;
using Service.Core.Result;
using Web.BML.AnalysisData;
using Web.BML.CoreData;
using Web.Models.AnalysisData;
using Web.Models.CoreData;
using Web.ViewModel.CoreData;
using System.IO;

namespace Web.Service
{
    public  class CompanyService : BaseService
    {
        
        private CompanyOp companyOp = CompanyOp.GetInstance();

        private static CompanyService instance;

        private CompanyService()
        {

        }

        public static CompanyService GetInstance()
        {
            if (instance == null)
            {
                instance = new CompanyService();
            }
            return instance;
        }

        /// <summary>
        /// 查询Company详细信息
        /// </summary>
        /// <param name="id">Company的id</param>
        /// <returns></returns>
        public CompanyModel QueryCompanyModel(String id)
        {
            Company company = companyOp.QueryCompany(id);
            return new CompanyModel().ConvertMondel(company, null);
        }

        
        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IServiceResult UpdateCompany(CompanyModel model)
        {
            if (model.CompanyId==null)
            {
                model.CompanyId = Guid.NewGuid().ToString();
                if(companyOp.QueryCompanyByName(model.Name)==null)
                {
                    Company company = model.ConvertEntity();
                    company.Logo = model.Logo;
                    companyOp.CreateCompany(company);
                    string path = Path.Combine(ConfigsService.XMLFILEPATH, company.Id);
                    string cur = HttpContext.Current.Server.MapPath("~");
                    Directory.CreateDirectory(cur+path);
                    File.Copy(cur + Path.Combine(ConfigsService.XMLFILEPATH, ConfigsService.DEFAULT), cur + Path.Combine(path, ConfigsService.DEFAULT));
                    ConfigsService.GetInstance().CreateReportConfig(model.CompanyId);
                    ConfigsService.GetInstance().CreateDefaultConfig(model.CompanyId);
                }
                else
                {
                    return Result(ResultType.ParamIsError, "您注册的公司名已存在");
                }
            }
            else
            {
                Company company = companyOp.QueryCompany(model.CompanyId);
                if (company == null)
                {
                    return Result(ResultType.ParamIsError,"操作有误，请尝试返回上一步重新操作");
                }
                model.ExpirationDate = company.ExpirationDate;
                model.Logo = company.Logo;
                company = model.ConvertEntity();
                companyOp.ModifCompany(company);
            }
            return Result(ResultType.Success);
        }

        /// <summary>
        /// 上传用户logo
        /// </summary>
        /// <param name="filename">LOGO图片地址</param>
        /// <returns></returns>
        public IServiceResult UploadLogo(String companyId, String filename)
        {
            if (string.IsNullOrEmpty(filename) || string.IsNullOrEmpty(companyId))
            {
                return Result(ResultType.ParamIsNull);
            }
            Company company = CompanyOp.GetInstance().QueryCompany(companyId);
            company.Logo = filename;
            companyOp.ModifCompany(company);
            return Result(ResultType.Success);
        }

        /// <summary>
        /// 添加公司
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CompanyModel AddCompany(CompanyModel model)
        {
            if (companyOp.QueryCompanyByName(model.Name) == null)
            {
                model.CompanyId = Guid.NewGuid().ToString();
                Company company = model.ConvertEntity();
                companyOp.CreateCompany(company);
                return model;
            }
            return null;
        }

        /// <summary>
        /// 获取所有公司
        /// </summary>
        /// <returns></returns>
        public List<CompanyModel> GetAllCompany()
        {
            List<User> users = UserOp.GetInstance().QueryAllUser();
            return companyOp.QueryAllCompanyList().Select(m => new CompanyModel().ConvertMondel(m, users)).ToList();
        }

        

    }
}